/**
 * 
 */
package com.SoapClasses;


import java.io.IOException;
import java.net.SocketTimeoutException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.util.Log;
import com.Data.Albums;
import com.Data.User;
import com.Managers.LogManagerClass;


/**
 * @author Chris
 * 
 */
public class SoapAlbums {

    private static String TAG = "SOAP ALBUMS";

    SoapSerializationEnvelope envelope;

    PropertyInfo pi;

    SoapObject Request;

    /**
     * @param data
     *            0 = NAME, 1 = PIN , 2 = ID USER
     * @return
     */
    public String addAlbum(String[] data) {

        String METHOD_NAME = "addAlbum";
        String SOAP_ACTION = "http://ShareUp.com/addAlbum";
        //
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);
        //
        String info = "";
        for (int i = 0; i < data.length; i++) {

            info += data[i] + "@";
            Log.e("data", data[i]);
        }
        Request.addProperty("data", info);
        //
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);
        //
        Object response = null;
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();
        }
        catch (SocketTimeoutException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return "-10";
        }
        catch (SoapFault e) {
            StackTraceElement[] stack = e.getStackTrace();
            String x = " ";
            for (StackTraceElement line : stack) {
                x += line.toString();
            }
            Log.e(TAG + ": ", x);
            LogManagerClass.writeLog(TAG + ": " + x);
        }
        catch (IOException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return x.getMessage();
        }
        catch (XmlPullParserException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return x.getMessage();
        }
        // IF EVERYTHING WENT OKAY IT GOING TO RETURN -1
        return response.toString();
    }

    /**
     * @param data
     *            0 = ID ALBUM, 1 = ID USER
     * @return
     */
    public String deleteAlbum(String[] data) {

        String METHOD_NAME = "deleteAlbum";
        String SOAP_ACTION = "http://ShareUp.com/deleteAlbum";
        //
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);
        //
        String info = "";
        for (int i = 0; i < data.length; i++) {
            info += data[i] + "@";
        }
        Log.e("ss", info);
        Request.addProperty("data", info);
        //
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);
        //
        Object response = null;
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();
        }
        catch (SocketTimeoutException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return "-10";
        }
        catch (SoapFault e) {
            StackTraceElement[] stack = e.getStackTrace();
            String x = " ";
            for (StackTraceElement line : stack) {
                x += line.toString();
            }
            Log.e(TAG + ": ", x);
            LogManagerClass.writeLog(TAG + ": " + x);
        }
        catch (IOException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return x.getMessage();
        }
        catch (XmlPullParserException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return x.getMessage();
        }
        // IF EVERYTHING WENT OKAY IT GOING TO RETURN -1
        return response.toString();
    }

    public Albums[] getAlbum(String idOwner, boolean singleone) {

        String METHOD_NAME = "getAlbums";
        String SOAP_ACTION = "http://ShareUp.com/getAlbums";
        //
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);

        Request.addProperty("id", idOwner);
        Request.addProperty("one", singleone);
        //
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);
        //
        Object response = null;
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();

            // IF EVERYTHING WENT OKAY IT GOING TO RETURN -1
            return getAlbumsData(response);
        }
        catch (SocketTimeoutException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            Albums[] als = new Albums[1];
            Albums al = new Albums();
            al.setID(-10);
            als[0] = al;
            return als;
        }
        catch (SoapFault e) {
            StackTraceElement[] stack = e.getStackTrace();
            String x = " ";
            for (StackTraceElement line : stack) {
                x += line.toString();
            }
            Log.e(TAG + ": ", x);
            LogManagerClass.writeLog(TAG + ": " + x);
            return null;
        }
        catch (IOException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            Albums[] als = new Albums[1];
            Albums al = new Albums();
            al.setID(-10);
            als[0] = al;
            return als;
        }
        catch (XmlPullParserException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            Albums[] als = new Albums[1];
            Albums al = new Albums();
            al.setID(-11);
            als[0] = al;
            return als;
        }

    }

    /**
     * @param data
     *            0 = ID ALBUM, 1 = ID OWNER, 2 = NAME , 3 PIN
     * @return
     */
    public String updateAlbum(String[] data) {

        String METHOD_NAME = "updateAlbum";
        String SOAP_ACTION = "http://ShareUp.com/updateAlbum";
        //
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);
        //
        String info = "";
        for (int i = 0; i < data.length; i++) {
            info += data[i] + "@";
        }
        Log.e("info SENDING", info);
        Request.addProperty("data", info);
        //

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);
        //
        Object response = null;
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();
        }
        catch (SocketTimeoutException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return "-10";
        }
        catch (SoapFault e) {
            StackTraceElement[] stack = e.getStackTrace();
            String x = " ";
            for (StackTraceElement line : stack) {
                x += line.toString();
            }
            Log.e(TAG + ": ", x);
            LogManagerClass.writeLog(TAG + ": " + x);
        }
        catch (IOException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return x.getMessage();
        }
        catch (XmlPullParserException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return x.getMessage();
        }
        // IF EVERYTHING WENT OKAY IT GOING TO RETURN -1
        return response.toString();
    }

    public String updateAlbumCover(String[] data) {

        String METHOD_NAME = "updateAlbumCover";
        String SOAP_ACTION = "http://ShareUp.com/updateAlbumCover";
        //
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);
        //
        String info = "";
        for (int i = 0; i < data.length; i++) {
            info += data[i] + "@";
        }
        Request.addProperty("data", info);
        //
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);
        //
        Object response = null;
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();
        }
        catch (SocketTimeoutException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return "-10";
        }
        catch (SoapFault e) {
            StackTraceElement[] stack = e.getStackTrace();
            String x = " ";
            for (StackTraceElement line : stack) {
                x += line.toString();
            }
            Log.e(TAG + ": ", x);
            LogManagerClass.writeLog(TAG + ": " + x);
        }
        catch (IOException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return x.getMessage();
        }
        catch (XmlPullParserException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return x.getMessage();
        }
        // IF EVERYTHING WENT OKAY IT GOING TO RETURN -1
        return response.toString();
    }

    private Albums[] getAlbumsData(Object sp) {

        String RES = sp.toString();

        try {
            JSONArray jsonArray = new JSONArray(RES);
            Albums[] als = new Albums[jsonArray.length()];
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jo = new JSONObject(jsonArray.getString(i));
                String name = jo.getString("Name");
                int pin = jo.getInt("Pin");
                int id = jo.getInt("ID");
                String Cover = jo.getString("CoverAlbum");
                String Date = jo.getString("Date");
                int aPrivacy = jo.getInt("AlbumPrivacy");
                int Count = jo.getInt("Count");
                // USER
                jo = new JSONObject(jsonArray.getJSONObject(i).getJSONObject("Owner").toString());
                User us = new User();
                int IDu = Integer.parseInt(jo.getString("ID"));

                String RealName = jo.getString("RealName");
                String Email = jo.getString("Email");
                // String Phone = jo.getString("Phone");
                String ProfilePicture = jo.getString("ProfilePicture");
                String CoverPicture = jo.getString("CoverPicture");
                int Sex = Integer.parseInt(jo.getString("Sex"));
                String Description = jo.getString("Description");
                int Follow = Integer.parseInt(jo.getString("Follow"));
                int Privacy = jo.getInt("Privacy");
                int Blur = jo.getInt("Blur");
                //
                us.setID(IDu);
                us.setEmail(Email);
                us.setIsFollowingMe(Follow);
                us.setRealName(RealName);
                us.setSex(Sex);
                us.setProfilePicture(ProfilePicture);
                us.setCoverPicture(CoverPicture);
                us.setDescription(Description);
                us.setPrivacy(Privacy);
                us.setBlur(Blur);
                //
                Albums al = new Albums();
                al.setID(id);
                al.setName(name);
                al.setPin(pin);
                al.setCover(Cover);
                al.setDate(Date);
                al.setOwner(us);
                al.setPrivacy(aPrivacy);
                al.setCount(Count);
                //
                als[i] = al;

            }

            return als;

        }
        catch (JSONException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());

            return null;
        }
    }
}
